{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "1e434b12-1b96-4f49-b7f4-cbe7eb41c7bb",
   "metadata": {
    "tags": []
   },
   "source": [
    "# Robodyno Stepper API"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "2b0e07da-21e8-4ca0-ad62-21147d9b0020",
   "metadata": {},
   "source": [
    "## 1.初始化Stepper对象"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "8ef03e56-477e-4530-af3c-436cb72a52a1",
   "metadata": {},
   "source": [
    "`StepperDriver(can, id_)`\n",
    "\n",
    "参数：\n",
    "- `can` : can总线接口\n",
    "- `id_` : Stepper驱动板模块ID，默认`0x22`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "87046e10-032e-4202-9d10-139bf870b199",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from robodyno.interfaces import CanBus\n",
    "from robodyno.components import StepperDriver\n",
    "can = CanBus()\n",
    "stepper = StepperDriver(can, 0x22)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "2f2d8e6f-284e-42d9-894e-9a346a7b9d49",
   "metadata": {},
   "source": [
    "## 2.读取步进电机版本"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "407c93c7-b41c-4149-ac26-8484a0f356d4",
   "metadata": {},
   "source": [
    "`get_version(timeout = 0)`\n",
    "\n",
    "参数：\n",
    "\n",
    "- timeout : 请求超时时间(s)，0代表无超时时间\n",
    "\n",
    "返回值 ：\n",
    "\n",
    "- API版本dict\n",
    "    - `main_version` : 主版本号\n",
    "    - `sub_version` : 副版本号\n",
    "    - `type` : 设备类型\n",
    "        - ROBODYNO_STEPPER_DRIVER"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2e8a40d2-de7b-468c-9b14-5b749bd08cb4",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'main_version': 0,\n",
       " 'sub_version': 3,\n",
       " 'type': <Model.ROBODYNO_STEPPER_DRIVER: 98>}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stepper.get_version()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "71f606c1-560e-4c82-81a4-048c23b8f362",
   "metadata": {},
   "source": [
    "## 3.设置CAN_ID"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "d21e75b3-8a1e-423b-afc8-c21dfb76da96",
   "metadata": {},
   "source": [
    "`config_can_bus(new_id)`\n",
    "\n",
    "参数 ：\n",
    "\n",
    "- `new_id` ：步进电机的新CAN_ID (默认：0x22)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "326bcf07-4971-4bc1-b256-107ee6119f00",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "stepper.config_can_bus(0x22)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "85a2cdc0-d254-4224-9eec-0e9483637093",
   "metadata": {},
   "source": [
    "## 4.设置细分"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "e391733a-83d5-48db-857c-7b8b9f2d5e11",
   "metadata": {},
   "source": [
    "`set_subdivision(subdivision)`\n",
    "\n",
    "参数 ：\n",
    "\n",
    "- `subdivision` ：步进电机细分 (可选：8，16，32，64)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "2bb2d316-c04f-4ec7-851d-450f6c3a0d76",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "stepper.set_subdivision(16)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "8edd009a-0f21-4134-a2a3-df327aeea44c",
   "metadata": {},
   "source": [
    "## 5.设置最大速度与加速度"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "b89a4f77-b156-4fd0-b7ce-7b8e65178195",
   "metadata": {},
   "source": [
    "`set_vel_acc_limit(max_vel, acc = None)`\n",
    "\n",
    "参数 ：\n",
    "\n",
    "- `max_vel` ：最大旋转速度(rad/s)\n",
    "- `acc` ：加速度(rad/s^2)\n",
    "    - 默认：`max_vel * 4`\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "1d24e0bb-2052-4294-b2d7-e6f93e725f4f",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "stepper.set_vel_acc_limit(1)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "b2cc2b16-c07c-4949-a6b8-ea8178fe0222",
   "metadata": {},
   "source": [
    "## 6.步进电机使能"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "a37be4f3-a3dd-41a2-8bde-9879ab6b05d5",
   "metadata": {},
   "source": [
    "`enable()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "d9ff6e52-ddc9-4f06-b0ba-62704ba4b17f",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "stepper.enable()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "e242edc0-8321-49d7-918f-089536dc0e13",
   "metadata": {},
   "source": [
    "## 7.步进电机失能"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "ccb11312-22f5-4bf4-ae37-156eafef0019",
   "metadata": {},
   "source": [
    "`disable()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "a63fd546-f618-4d0a-9369-cecb0789c025",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "stepper.disable()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "31524e59-222a-48a5-aae7-725587897c53",
   "metadata": {},
   "source": [
    "## 8.电机软急停"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "c357450b-22e9-4103-a08a-7fbb96c524c3",
   "metadata": {},
   "source": [
    "`stop()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "449ecd7f-1e32-4ada-9172-5465d9411ea6",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "stepper.stop()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "dc6f9921-66c0-4614-a5a3-583b52bd0caf",
   "metadata": {},
   "source": [
    "## 9.设置位置"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "39db0599-0155-47cd-9de3-d3d07d25803e",
   "metadata": {},
   "source": [
    "`set_pos(pos)`\n",
    "\n",
    "参数:\n",
    "\n",
    "- `pos`: 目标位置(rad)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "f5832462-b7a7-4a93-8c4d-89506b589531",
   "metadata": {},
   "outputs": [],
   "source": [
    "stepper.set_pos(10)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "3327b9cc-1690-4731-91cb-ef6a0d29eae1",
   "metadata": {},
   "source": [
    "## 10.设置速度"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "a09d884d-7c02-40e8-ae12-a8376e971d23",
   "metadata": {},
   "source": [
    "`set_vel(vel)`\n",
    "\n",
    "参数：\n",
    "\n",
    "- `vel`: 目标速度(rad/s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "7f6bbec4-94ed-4015-aa2c-889e50d9cc04",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "stepper.set_vel(1)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "318fd78a-0859-448f-b219-d6c0ed26c322",
   "metadata": {},
   "source": [
    "## 11.读取电机位置"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "de4b60f9-1634-45eb-9d11-d997ac3bd986",
   "metadata": {},
   "source": [
    "`get_pos(timeout=None)`\n",
    "\n",
    "参数：\n",
    "\n",
    "- `timeout` : 请求超时时间(s)，0代表无超时时间\n",
    "\n",
    "返回值：\n",
    "\n",
    "- 当前位置(rad)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "22ea91da-f678-4068-a094-7f25b9ca3d46",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9.999689416376311"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stepper.get_pos()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "5b724938-0a99-49bd-b191-f5b387d99fba",
   "metadata": {},
   "source": [
    "## 12.读取电机速度"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "d7ba8955-7e9c-4f3c-a3e5-80938802086d",
   "metadata": {},
   "source": [
    "`get_vel(timeout=None)`\n",
    "\n",
    "参数：\n",
    "\n",
    "- `timeout` : 请求超时时间(s)，0代表无超时时间\n",
    "\n",
    "返回值：\n",
    "\n",
    "- 当前速度(rad/s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "8f7c952b-f31c-4dc5-be52-de5b61f21951",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stepper.get_vel()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
